{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串的多种写法\n",
    "b = \"双引号\"\n",
    "\n",
    "d = \"\"\"\n",
    "三双引号\n",
    "换行\n",
    "\"\"\"\n",
    "print(d)  # 支持自动换行\n",
    "\n",
    "\"\"\"\n",
    "这样也可以写注释\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串切片\n",
    "\n",
    "a = \"abcdefgh\"\n",
    "# [开始索引:结束索引:步长]\n",
    "print(a[0])  # a\n",
    "print(a[1])  # b\n",
    "print(a[2])  # c\n",
    "print(a[2:5])  # cde\n",
    "print(a[2:])  # cdefgh\n",
    "print(a[:5])  # abcde\n",
    "print(a[:])  # abcdefgh\n",
    "print(a[0::1])  # abcdefgh\n",
    "print(a[0::2])  # aceg # 间隔取\n",
    "print(a[1::2])  # bdfh # 间隔取\n",
    "print(a[0::3])  # adg # 间隔取\n",
    "print(a[0:4:2])  # ac # 间隔取\n",
    "print(a[::-1])  # 反向\n",
    "print(\"超索引:\" + a[100:101])  # 为空 #它不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串中查找\n",
    "\n",
    "a = \"abcdefgh\"\n",
    "print(a.find(\"a\"))  # 返回索引0\n",
    "print(a.find(\"c\"))  # 返回索引2\n",
    "print(a.find(\"i\"))  # 找不到则返回-1\n",
    "print(a.find(\"abc\"))  # 返回a的索引0\n",
    "print(a.find(\"bcd\"))  # 返回b的索引1\n",
    "print(a.index(\"a\"))  # 0\n",
    "print(a.index(\"b\"))  # 1\n",
    "# print(a.index(\"i\"))  # index找不到的时候会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分割字符串\n",
    "\n",
    "a = \"abc_def_ghi_jkl\"\n",
    "aList = a.split(\"_\")\n",
    "print(aList)  # ['abc', 'def', 'ghi', 'jkl']\n",
    "\n",
    "aList = a.split(\"_\", 2)  # 可设置分割次数,实际返回3个元素\n",
    "print(aList)  # ['abc', 'def', 'ghi_jkl']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 拼接字符串\n",
    "\n",
    "print(\"abc\" + \"123\")\n",
    "\n",
    "print(\"abc\" * 9)  # 字符串可以通过乘法快速复制\n",
    "\n",
    "间隔符 = \"_\"  # 设置间隔符号\n",
    "b = 间隔符.join(\"abcdefg\")  # a通过join方法快速生成带指定间隔的字符串\n",
    "print(b)  # a_b_c_d_e_f_g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 全部转为大小写\n",
    "print(\"abc\".upper())  # ABC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断是否为纯数字\n",
    "print(\"abc\".isdigit())  # False\n",
    "print(\"123\".isdigit())  # True\n",
    "print(\"abc123\".isdigit())  # False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 清除空格\n",
    "print(\"abc   \".strip())  # abc\n",
    "print(\"   123\".strip())  # 123\n",
    "print(\"   abc   \".lstrip())  # 定向清除左侧空格\n",
    "print(\"   abc   \".rstrip())  # 定向清除左侧空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 插入变量\n",
    "w = \"文本\"\n",
    "s = 123\n",
    "print(\"第一个参数是{},第二个参数是{}\".format(w, s))  # 第一个参数是文本,第二个参数是123\n",
    "print(f\"第一个参数是{w},第二个参数是{s}\")  # 第一个参数是文本,第二个参数是123"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
